"""
get_video_info.py
获取视频详细信息
"""
import cv2


def get_video_info(video_path):
    """获取视频信息"""
    cap = cv2.VideoCapture(video_path)

    if not cap.isOpened():
        print(f"❌ 无法打开视频: {video_path}")
        return

    # 获取视频属性
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    duration = frame_count / fps if fps > 0 else 0

    # 获取编码格式
    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
    codec = "".join([chr((fourcc >> 8 * i) & 0xFF) for i in range(4)])

    print("=" * 60)
    print("视频信息")
    print("=" * 60)
    print(f"文件路径: {video_path}")
    print(f"分辨率: {width} x {height} 像素")
    print(f"帧率: {fps:.2f} FPS")
    print(f"总帧数: {frame_count}")
    print(f"时长: {duration:.2f} 秒 ({duration / 60:.2f} 分钟)")
    print(f"编码格式: {codec}")
    print("=" * 60)

    cap.release()

    return {
        'width': width,
        'height': height,
        'fps': fps,
        'frame_count': frame_count,
        'duration': duration,
        'codec': codec
    }


# 使用示例
if __name__ == "__main__":
    video_path = r"E:\zhihao\2025文档\25年10月\Captures\消防通道\消防通道_1.mp4"
    # video_path = "your_video.mp4"  # 修改为你的视频路径
    info = get_video_info(video_path)